package com.pphh.demo.rabbit.cluster;

import com.pphh.demo.amqp.MqConsumer;
import com.pphh.demo.amqp.MqService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Please add description here.
 *
 * @author huangyinhuang
 * @date 2019/3/27
 */
@RestController
@RequestMapping(value = "/api", produces = "application/json; charset=utf-8")
public class ClusterController {

    private final static Logger log = LoggerFactory.getLogger(ClusterController.class);

    @Autowired
    private MqService mqService;
    @Autowired
    private MqConsumer mqConsumer;

    @RequestMapping(value = "hello", method = RequestMethod.GET)
    public String test() {
        log.info("this is a test controller");
        return "hello,world";
    }

    @RequestMapping(value = "mq/loop", method = RequestMethod.POST)
    public Boolean sendMsg(@RequestParam(required = false) Integer delayed, @RequestBody String msg) {
        log.info("test rabbit mq - send a loop message");
        Boolean bSuccess;
        if (delayed != null) {
            bSuccess = mqService.sendDelayedMessage(msg, 15000);
        } else {
            bSuccess = mqService.sendMessage(msg);
        }
        return bSuccess;
    }

    @RequestMapping(value = "mq/send", method = RequestMethod.POST)
    public Boolean sendMsg2(@RequestBody String msg) {
        log.info("test rabbit mq - send message");
        return mqService.sendMessageForPull(msg);
    }

    @RequestMapping(value = "mq/pull", method = RequestMethod.POST)
    public String pullMsg() {
        log.info("test rabbit mq - pull message");
        return mqConsumer.getMessage();
    }

}
